package com.yjdb.sanyuantang.modules.Admin.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaIgnore;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yjdb.sanyuantang.aop.OperationLogAnnotation;
import com.yjdb.sanyuantang.common.result.CommonResult;
import com.yjdb.sanyuantang.common.result.ResultCode;
import com.yjdb.sanyuantang.modules.Admin.service.AdminUserService;
import com.yjdb.sanyuantang.modules.Admin.DTO.admin.AdminAddDTO;
import com.yjdb.sanyuantang.modules.Admin.DTO.admin.AdminUpdateDTO;
import com.yjdb.sanyuantang.modules.Admin.DTO.admin.AdminPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * packageName com.yjdb.sanyuantang.modules.Admin.controller
 *
 * @author 混沌酱
 * @version 1.0
 * @className AdminController
 * @date 2024/3/14
 * @description TODO
 */
@Tag(name = "后台用户管理")
@RequestMapping("/admin/user")
@RestController
@SaCheckPermission("sys.admin")
public class AdminUserController {

    private static final Logger logger = LoggerFactory.getLogger(AdminUserController.class);

    @Resource
    private AdminUserService adminUserService;

    @SaCheckPermission("sys.admin*add")
    @OperationLogAnnotation(operModule = "后台用户管理", operFunction = "添加用户")
    @Operation(summary = "添加用户", description = "添加用户")
    @RequestMapping(path = "/add", method = RequestMethod.POST)
    public CommonResult<?> add(@RequestBody AdminAddDTO adminAddDTO) {
        try {
            adminUserService.add(adminAddDTO);
            return CommonResult.success(ResultCode.SUCCESS,"用户:"+ adminAddDTO.getUsername()+"添加成功");
        } catch (Exception e) {
            logger.debug("添加失败", e);
            return CommonResult.failed(e.getMessage());
        }
    }
    @SaCheckPermission("sys.admin*edit")
    @OperationLogAnnotation(operModule = "后台用户管理", operFunction = "修改用户")
    @Operation(summary = "修改用户", description = "修改用户")
    @RequestMapping(path = "/updata", method = RequestMethod.POST)
    public CommonResult<?> updata(@RequestBody AdminUpdateDTO adminUpdateDTO) {
        try {
            adminUserService.updata(adminUpdateDTO);
            return CommonResult.success(ResultCode.SUCCESS,"用户:"+ adminUpdateDTO.getUsername()+"修改成功");
        } catch (Exception e) {
            logger.debug("修改失败", e);
            return CommonResult.failed(e.getMessage());
        }
    }

    @SaCheckPermission("sys.admin*delete")
    @OperationLogAnnotation(operModule = "后台用户管理", operFunction = "删除用户")
    @Operation(summary = "删除用户", description = "删除用户")
    @RequestMapping(path = "/delete", method = RequestMethod.GET)
    public CommonResult<?> delete(
            @Parameter(name = "id",description = "用户id",required = true)  Integer id,
            @Parameter(name = "username",description = "用户名",required = true)  String username
    ) {
        try {
            adminUserService.delete(id, username);
            return CommonResult.success(ResultCode.SUCCESS,"用户:"+username+"删除成功");
        } catch (Exception e) {
            logger.debug("删除失败", e);
            return CommonResult.failed(e.getMessage());
        }

    }

    // 启用或关闭用户
    @SaCheckPermission("sys.admin*enable")
    @OperationLogAnnotation(operModule = "后台用户管理", operFunction = "启用或关闭用户")
    @Operation(summary = "启用或关闭用户", description = "启用或关闭用户")
    @RequestMapping(path = "/enable", method = RequestMethod.GET)
    public CommonResult<?> enable(
            @Parameter(name = "id", description = "用户id",required = true) Integer id,
            @Parameter(name = "username", description = "用户名",required = true) String username,
            @Parameter(name = "enable", description = "0 关闭 1开启",required = true) String enable
    ) {
        try {
            adminUserService.enable(id, username, enable);
            return CommonResult.success(ResultCode.SUCCESS,"用户:"+username+"启用或关闭成功");
        } catch (Exception e) {
            logger.debug("启用或关闭失败", e);
            return CommonResult.failed(e.getMessage());
        }
    }


    // 查询用户列表 分页查询
    @Operation(summary = "查询用户列表", description = "查询用户列表")
    @RequestMapping(path = "/list", method = RequestMethod.POST)
    public CommonResult<PageInfo<AdminUpdateDTO>> list(@RequestBody AdminPage adminPage) {
        try {
            PageHelper.startPage(adminPage.getPageNum(), adminPage.getPageSize());
            List<AdminUpdateDTO> list = adminUserService.list(adminPage);
            PageInfo<AdminUpdateDTO> listPage = new PageInfo<>(list);
            return CommonResult.success(listPage);
        } catch (Exception e) {
            logger.debug("查询失败", e);
            return CommonResult.failed(e.getMessage());
        }
    }


    //绑定角色
    @SaCheckPermission("sys.admin*bindRole")
    @OperationLogAnnotation(operModule = "后台用户管理", operFunction = "绑定角色")
    @Operation(summary = "绑定角色", description = "绑定角色")
    @RequestMapping(path = "/bindRole", method = RequestMethod.GET)
    public CommonResult<?> bindRole(
            @Parameter(name = "id", description = "用户id",required = true) Integer id,
            @Parameter(name = "username", description = "用户名",required = true) String username,
            @Parameter(name = "roleIds", description = "角色id(角色id空字符串表示解绑所有的角色)",required = true) String roleIds,
            @Parameter(name = "roleNames", description = "角色名称",required = true) String roleNames
    ) {
        try {
            adminUserService.bindRole(id, roleIds);
            return CommonResult.success(ResultCode.SUCCESS,"用户:"+username+"成功绑定角色:"+roleNames);
        } catch (Exception e) {
            logger.debug("绑定角色失败", e);
            return CommonResult.failed(e.getMessage());
        }
    }

}
